{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e5f61bf",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# pip install transformers datasets torch pillow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7f7e9d8-62af-4d04-acfc-e874165b9f17",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load resnet-50 model and run forward pass on an image.\n",
    "\n",
    "from transformers import AutoImageProcessor, ResNetForImageClassification\n",
    "import torch\n",
    "from datasets import load_dataset\n",
    "\n",
    "dataset = load_dataset(\"huggingface/cats-image\")\n",
    "image = dataset[\"test\"][\"image\"][0]\n",
    "\n",
    "processor = AutoImageProcessor.from_pretrained(\"microsoft/resnet-50\")\n",
    "model = ResNetForImageClassification.from_pretrained(\"microsoft/resnet-50\")\n",
    "\n",
    "inputs = processor(image, return_tensors=\"pt\")\n",
    "\n",
    "with torch.no_grad():\n",
    "    result = model(**inputs, output_hidden_states=True)\n",
    "    logits = result.logits\n",
    "\n",
    "# model predicts one of the 1000 ImageNet classes\n",
    "predicted_label = logits.argmax(-1).item()\n",
    "print(model.config.id2label[predicted_label])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff5e0095",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Visualize the hidden states as heatmaps.\n",
    "\n",
    "from tensor_visualizer import TensorVisualizer\n",
    "TensorVisualizer(result.hidden_states[2][0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
